package com.neusoft.neu24.nep.grid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.neu24.nep.entity.ComonResult;
import com.neusoft.neu24.nep.entity.ResultCode;
import com.neusoft.neu24.nep.grid.entity.Grid;
import com.neusoft.neu24.nep.grid.mapper.GridMapper;
import com.neusoft.neu24.nep.grid.service.IGridService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域网格信息 服务实现类
 * </p>
 *
 * @author 段静迪，duanjingdi@163.com
 * @since 2024-05-29
 */
@Service
public class GridServiceImpl extends ServiceImpl<GridMapper, Grid> implements IGridService {
    @Autowired
    private GridMapper gridMapper;

    @Override
    public ComonResult addGrid(Grid grid) {
        try {
            boolean b = save(grid);
            if (b) {
                return ComonResult.success(grid.getId());
            }
            else {
                return ComonResult.failed(ResultCode.INSERT_FAILED);
            }
        } catch (Exception ex) {
            return ComonResult.failed(500, ex.getMessage());
        }

    }
    @Override
    public ComonResult selectGridByPid(Integer pid) {
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq( Grid::getPid , pid   ).eq(Grid::getStatus, 1);
        List<Grid> list = list(lambdaQueryWrapper);

        if (list ==null ||  list.size() ==0){
            return   ComonResult.failed(ResultCode.RESULTSET_IS_EMPTY);
        }
        return ComonResult.success(list);
    }

    @Override
    public ComonResult selectGridByIds(int[] is) {
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Integer> l = Arrays.stream(is).boxed().collect(Collectors.toList());
        lambdaQueryWrapper.in( Grid::getId , l ).orderByDesc( Grid::getGname);
        List<Grid> list = list(lambdaQueryWrapper);

        if (list ==null ||  list.size() ==0){
            return   ComonResult.failed(ResultCode.RESULTSET_IS_EMPTY);
        }
        return ComonResult.success(list);
    }

    @Override
    public ComonResult selectPagedGrid(long current, long size) {
        IPage<Grid>  pagec =new Page<>(  current,   size);
        LambdaQueryWrapper<Grid>  queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.ge( Grid::getId , 6  );
        IPage<Grid> pages = getBaseMapper().selectPage(pagec, queryWrapper);
        return ComonResult.success(pages);
    }

    @Override
    public ComonResult<Grid> selectAllGridTree() {
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        int pid = 0;
        lambdaQueryWrapper.eq( Grid::getPid , pid   );
        Grid root =  getBaseMapper().selectOne(  lambdaQueryWrapper );
        fillChildren(root);
        return ComonResult.success(root);
    }

    private void fillChildren(Grid root) {

        int  pid =  root.getId();
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq( Grid::getPid , pid   );
        root.setChildren(   getBaseMapper().selectList(lambdaQueryWrapper));

        if( root.getLevel() >=3 ||  root.getChildren() == null ||  root.getChildren().size() ==0 ){
            return;
        }
        if( root.getLevel() < 3   ){
            root.getChildren().forEach(  (grid)->{
                fillChildren(grid);
            });
        }
    }


    public ComonResult selectGridByPid1(Integer pid) {
         List<Grid> lgs =  gridMapper.selectGridByPid(pid);
         if (lgs ==null ||  lgs.size() ==0){
             return   ComonResult.failed(ResultCode.RESULTSET_IS_EMPTY);
         }
        return ComonResult.success(lgs);
    }
}
